﻿#region Using directives
using System;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web.UI;
using System.Xml;
using ProcolDemo.I18N;

#endregion

namespace ProcolDemo {
    public partial class Start : Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (!Common.IsUserLoggedIn(Session))
                Response.Redirect("Default.aspx");

            Common.FillI18NStrings(this, Session);

            var I18N = new I18NService();
            int uid = Common.GetUserId(Session);

            var lbls = new[] { lblSystem, lblMyIdeas, lblMyMembers, lblMyProjects };

            foreach (var lbl in lbls) {
                String type = lbl == lblMyIdeas
                                      ? "ideas"
                                      : (lbl == lblMyMembers ? "members" : (lbl == lblMyProjects ? "projects" : "system"));
                String url = String.Format("http://localhost:1059/RssGenerator.aspx?Uid={0}&type={1}", uid, type);
                var rss = new Rss20FeedFormatter();

                rss.ReadFrom(XmlReader.Create(url,
                                              new XmlReaderSettings {
                                                  IgnoreWhitespace = true,
                                                  CheckCharacters = true,
                                                  CloseInput = true,
                                                  IgnoreComments = true,
                                                  IgnoreProcessingInstructions = true,
                                                  ProhibitDtd = false
                                              }));

                int count = rss.Feed.Items.Count();
                lbl.Text = count == 0
                                   ? I18N.GetTextAccordingToUserId(uid, "NEWS_NONEWS")
                                   : String.Format(I18N.GetTextAccordingToUserId(uid, "NEWS_YESNEWS"), count);

                if (count > 0) {
                    switch (type) {
                        case "ideas":
                            var ideas = from i in rss.Feed.Items
                                        orderby i.PublishDate descending
                                        select new { FeedTitle = i.Title.Text, FeedContent = i.Summary.Text, FeedLink = i.Links[0].Uri };

                            rptIdeas.DataSource = ideas;
                            rptIdeas.DataBind(); 
                            
                            break;
                        case "members":
                            var members = from i in rss.Feed.Items
                                          orderby i.PublishDate descending
                                          select new { FeedTitle = i.Title.Text, FeedContent = i.Summary.Text, FeedLink = i.Links[0].Uri };

                            rptMembers.DataSource = members;
                            rptMembers.DataBind(); 
                            
                            break;
                        case "projects":
                            var projects = from i in rss.Feed.Items
                                           orderby i.PublishDate descending
                                           select new { FeedTitle = i.Title.Text, FeedContent = i.Summary.Text, FeedLink = i.Links[0].Uri };

                            rptProjects.DataSource = projects;
                            rptProjects.DataBind();

                            break;
                        case "system":
                            var system = from i in rss.Feed.Items
                                         orderby i.PublishDate descending
                                         select new { FeedTitle = i.Title.Text, FeedContent = i.Summary.Text, FeedLink = i.Links[0].Uri };

                            rptSystem.DataSource = system;
                            rptSystem.DataBind(); 
                            
                            break;
                    }
                }
            }
        }
    }
}